package com.base.cn.platform.os.controller.course.material;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.TT.TTServiceUtils;
import com.base.cn.platform.os.common.utils.course.MaterialType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.material.CouMaterial;
import com.base.cn.platform.os.entity.course.material.CouMaterialCondition;
import com.base.cn.platform.os.service.course.material.CouMaterialBiz;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 素材Controller
 *
 * @author s.li
 * @create 2018-04-19-18:05
 */
@RestController
@RequestMapping("/course")
public class CouMaterialController extends BaseController {

    @Autowired
    private CouMaterialBiz couMaterialBiz;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private TTServiceUtils ttServiceUtils;

    /**
     * 验证素材的播放码是否存在
     * @param condition 查询条件
     * @return Map<String,Object>
     */
    @RequestMapping("/checkingMaterialCode")
    public Map<String,Object>  checkingMaterialCode(@RequestBody CouMaterialCondition condition){
        if(!condition.getTypeKey().equals(MaterialType.TYPE_KEY_ARTICLE.getKey()) && StringUtils.isNotEmpty(condition.getPlayUrl())){
            CouMaterial material = couMaterialBiz.findOne(" playUrl = '"+condition.getPlayUrl().trim()+"'",null);
            if(ObjectUtils.isNotEmpty(material)){
                return ResultUtil.ERROR("播放码已存在于【"+material.getName()+"】素材中，确认还需要创建吗？");
            }
        }
        return ResultUtil.SUCCESS();
    }

    /**
     * 查询素材数量
     * @param condition 查询条件
     * @return 数量
     */
    @RequestMapping("/materialCount")
    public BigDecimal count(@RequestBody CouMaterialCondition condition){
        return couMaterialBiz.count(condition);
    }

    /**
     * 保存素材数据
     * @param couMaterial 素材对象
     * @return Map<String,Object>
     */
    @RequestMapping("/saveMaterial")
    public Map<String,Object> saveMaterial(@RequestBody CouMaterial couMaterial ){
        if(couMaterial.getTypeKey().equals(MaterialType.TYPE_KEY_VIDEO.getKey())
                || couMaterial.getTypeKey().equals(MaterialType.TYPE_KEY_AUDIO.getKey())){//如果是视频类型或音频
            if(StringUtils.isEmpty(couMaterial.getPlayUrl())){
                if(couMaterial.getTypeKey().equals(MaterialType.TYPE_KEY_VIDEO.getKey())){
                    return ResultUtil.ERROR("请填写对应视频码");
                }else{
                    return ResultUtil.ERROR("请填写对应音频码");
                }
            }
        }
        if(couMaterial.getTypeKey().equals(MaterialType.TYPE_KEY_LIVE.getKey())){//如果是直播类型
            TreeMap<String, String> configMap = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
            if ("TT".equals(configMap.get("type")) && StringUtils.isEmpty(couMaterial.getId())){ //新建直播间
                String type = couMaterial.getLiveBilling().intValue() == 1 ?"1":couMaterial.getLiveType();
                Map resultMap = ttServiceUtils.create3TLiveData(couMaterial.getLiveName(),couMaterial.getLiveNotice(),couMaterial.getLiveBeginTime(),couMaterial.getLiveEndTime(),type,couMaterial.getTeacherId(),couMaterial.getAssistantId());
                if((boolean) resultMap.get("success")){
                    Map entity = (Map) resultMap.get("entity");
                    couMaterial.setRoomId(entity.get("classId").toString());
                    couMaterial.setVideoDuration((couMaterial.getLiveEndTime().getTime()-couMaterial.getLiveBeginTime().getTime())/1000);
                }else{
                    return resultMap;
                }
            }
            if(ObjectUtils.isEmpty(couMaterial.getLiveBilling())){
                return ResultUtil.ERROR("请选择直播房间类型");
            }
            if(!"TT".equals(configMap.get("type"))){
                if(StringUtils.isEmpty(couMaterial.getPlayUrl())){
                    return ResultUtil.ERROR("请填写直播间识别码");
                }
                if(StringUtils.isEmpty(couMaterial.getRoomId()) && couMaterial.getPlayType().equals("96K")){
                    return ResultUtil.ERROR("请填写直播间ID");
                }
                if(StringUtils.isEmpty(couMaterial.getTeacherCode()) && couMaterial.getPlayType().equals("96K")){
                    return ResultUtil.ERROR("请填写讲师参加码");
                }
                if(StringUtils.isEmpty(couMaterial.getAdminCode()) && couMaterial.getPlayType().equals("96K")){
                    return ResultUtil.ERROR("请填写管理员参加码");
                }
            }
            if(StringUtils.isEmpty(couMaterial.getLiveBeginTime())){
                return ResultUtil.ERROR("请选择直播开始时间");
            }
            if(StringUtils.isEmpty(couMaterial.getLiveEndTime())){
                return ResultUtil.ERROR("请选择直播结束时间");
            }
            if(couMaterial.getLiveBeginTime().after(couMaterial.getLiveEndTime())){
                return ResultUtil.ERROR("直播开始时间不能大于结束时间");
            }
        }
        if(StringUtils.isEmpty(couMaterial.getName())){
            return ResultUtil.ERROR("请填写素材名称");
        }else{
            if(couMaterial.getName().trim().length()>30){
                return ResultUtil.ERROR("素材名称限制在30个字以内");
            }
        }
        String message = StringUtils.checkSubjectCondition(couMaterial.getSubjectIds());
        if(message !=null){
            return ResultUtil.ERROR(message);
        }
        if(couMaterial.getTypeKey().equals(MaterialType.TYPE_KEY_ARTICLE.getKey())){//文章类型的素材
            if(StringUtils.isEmpty(couMaterial.getArticleSummary())){
                return ResultUtil.ERROR("请填写文章简介");
            }
            if(StringUtils.isEmpty(couMaterial.getArticleContext())){
                return ResultUtil.ERROR("请填写文章内容");
            }
            if(ObjectUtils.isEmpty(couMaterial.getVideoDuration())){
                return ResultUtil.ERROR("请填写读取该文章应有的时长");
            }
        }
        if(!DataUtil.idIsNotNull(couMaterial.getTeacherId())){
            return ResultUtil.ERROR("请选择讲师");
        }
        couMaterial = couMaterialBiz.saveMaterial(couMaterial);
        return ResultUtil.SUCCESS("素材数据保存成功",couMaterial);
    }

    /**
     * 分页查询素材数据
     * @param couMaterialCondition 查询条件对象
     * @param currentPage 当前页码
     * @param setSubjectData
     * @param setTeacherData
     * @return PageInfo<CouMaterial>
     */
    @RequestMapping("/findMaterialPage")
    public PageInfo<CouMaterial> findMaterialPage(@RequestBody CouMaterialCondition couMaterialCondition,
                                                  @RequestParam("currentPage") int currentPage,
                                                  @RequestParam("setSubjectData") boolean setSubjectData,
                                                  @RequestParam("setTeacherData") boolean setTeacherData){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        page.setPageSize(14);
        return couMaterialBiz.findMaterialPage(couMaterialCondition,page,setSubjectData,setTeacherData);
    }

    /**
     * 多条件查询素材数据列表
     * @param condition 查询条件
     * @param setSubjectData
     * @param setTeacherData
     * @return List<CouMaterial>
     */
    @RequestMapping("/findMaterialList")
    public List<CouMaterial> findMaterialList(@RequestBody CouMaterialCondition condition,
                                              @RequestParam("setSubjectData") boolean setSubjectData,
                                              @RequestParam("setTeacherData") boolean setTeacherData){
        return couMaterialBiz.findMaterialList(condition,setSubjectData,setTeacherData);
    }

    /**
     * 通过多条件查询素材Mpa
     * @param condition 查询条件
     * @param setSubjectData
     * @param setTeacherData
     * @return Map<BigDecimal,CouMaterial>
     */
    @RequestMapping("/findMaterialToIdMap")
    public Map<BigDecimal,CouMaterial> findMaterialToIdMap(@RequestBody CouMaterialCondition condition,
                                                           @RequestParam("setSubjectData") boolean setSubjectData,
                                                           @RequestParam("setTeacherData") boolean setTeacherData){
        return couMaterialBiz.findMaterialToIdMap(condition,setSubjectData,setTeacherData);
    }

    /**
     * 通过ID，查询素材对象
     * @param id ID
     * @param setSubjectData
     * @param setTeacherData
     * @return
     */
    @RequestMapping("/findMaterialById")
    public CouMaterial findMaterialById(@RequestParam("id") BigDecimal id,
                                        @RequestParam("setSubjectData") boolean setSubjectData,
                                        @RequestParam("setTeacherData") boolean setTeacherData){
        return couMaterialBiz.findMaterialById(id,setSubjectData,setTeacherData);
    }

    /**
     * 通过ID串，查询素材列表
     * @param ids ID串
     * @param setSubjectData
     * @param setTeacherData
     * @return
     */
    @RequestMapping("/findMaterialByIds")
    public List<CouMaterial> findMaterialByIds(@RequestParam("ids") String ids,
                                               @RequestParam("setSubjectData") boolean setSubjectData,
                                               @RequestParam("setTeacherData") boolean setTeacherData){
        return couMaterialBiz.findMaterialByIds(ids,setSubjectData,setTeacherData);
    }

    /**
     * 通过ID串，查询素材Map
     * @param ids 尖串
     * @param setSubjectData
     * @param setTeacherData
     * @return
     */
    @RequestMapping("/findMaterialByIdsToMap")
    public Map<BigDecimal,CouMaterial> findMaterialByIdsToMap(@RequestParam("ids") String ids,
                                                              @RequestParam("setSubjectData") boolean setSubjectData,
                                                              @RequestParam("setTeacherData") boolean setTeacherData){
        return couMaterialBiz.findMaterialByIdsToMap(ids,setSubjectData,setTeacherData);
    }

    /**
     * 修改素材状态
     * @param materialIds 素材ID串，多个ID以“,”隔开
     * @param status 状态，1未发布，2已发布，3删除
     * @return Map<String,Object>
     */
    @RequestMapping("/updateMaterialStatus")
    public Map<String,Object> updateMaterialStatus(@RequestParam("materialIds") String materialIds,
                                                   @RequestParam("status") int status){
        if(StringUtils.isNotEmpty(materialIds)) {
            materialIds = StringUtils.subHeadTailString(materialIds, ",");
            if (StringUtils.isNotEmpty(materialIds)) {
                List<String> containColumns = new ArrayList<>();
                containColumns.add("id");
                containColumns.add("status");
                //先获取课程数据
                List<CouMaterial> couMaterialList = couMaterialBiz.find(" id in ("+materialIds+")",null,containColumns);
                if(ObjectUtils.isEmpty(couMaterialList)){
                    return ResultUtil.ERROR("课程数据不存在");
                }
                List<CouMaterial> updateList = new ArrayList<>();
                List<CouMaterial> deleteList = new ArrayList<>();
                //通过状态分组
                Map<Integer,List<CouMaterial>> couMaterialListMap = couMaterialList.stream().collect(Collectors.groupingBy(e->e.getStatus()));
                List<CouMaterial> couMaterialList0 = couMaterialListMap.get(0);//获取未指定
                List<CouMaterial> couMaterialList1 = couMaterialListMap.get(1);//获取未发布的
                List<CouMaterial> couMaterialList2 = couMaterialListMap.get(2);//获取已发布
                if(status ==1){//未发布
                    if(ObjectUtils.isNotEmpty(couMaterialList2)){
                        updateList.addAll(couMaterialList2);
                    }
                }
                if(status ==2){//已发布
                    if(ObjectUtils.isNotEmpty(couMaterialList1)){
                        updateList.addAll(couMaterialList1);//如果有则添加
                    }
                    if(ObjectUtils.isNotEmpty(couMaterialList0)){
                        updateList.addAll(couMaterialList0);//如果有则添加
                    }
                }
                if(status==3){//删除
                    //真删除
                    if(ObjectUtils.isNotEmpty(couMaterialList0)) {//如果是上架的课程
                        deleteList.addAll(couMaterialList0);//如果有则添加
                    }
                    //修改状态
                    if(couMaterialList1!=null && couMaterialList1.size()>0) {//如果是上架的课程
                        updateList.addAll(couMaterialList1);//如果有则添加
                    }
                    if(ObjectUtils.isNotEmpty(couMaterialList2)){//如果是下架的课程
                        updateList.addAll(couMaterialList2);//如果有则添加
                    }
                }
                couMaterialBiz.updateMaterialStatus(updateList,deleteList,status);
            }
        }
        String message = "";
        if(status ==1){
            message = "素材取消发布成功";
        }
        if(status==2){
            message = "素材取消发布成功";
        }
        if(status==3){
            message = "素材删除成功";
        }
        return ResultUtil.SUCCESS(message);
    }

    /**
     * 修改浏览/播放状态
     * @param materialIds 素材ID串
     * @param status 状态，1正常，2冻结
     * @return Map<String,Object>
     */
    @RequestMapping("/updateMaterialPlayStatus")
    public Map<String,Object> updateMaterialPlayStatus(@RequestParam("materialIds") String materialIds,
                                                       @RequestParam("status") int status){
        couMaterialBiz.updateMaterialPlayStatus(materialIds,status);
        return ResultUtil.SUCCESS("素材的浏览/播放状态修改成功");
    }


    /**
     * 查询今日直播素材
     * @param userId 用户ID
     * @return
     */
    @RequestMapping("/listTodayMaterialLive")
    public List<Map<String, Object>> listTodayMaterialLive(@RequestParam("userId") BigDecimal userId){
        return couMaterialBiz.listTodayMaterialLive(userId);
    }

    /**
     * 下载导入模板
     * @param materialType 素材类型，视频/音频
     * @param subjectIds 专业ID串
     * @return 返回模板文件
     * @throws Exception
     */
    @RequestMapping("/downloadMaterialTemplate")
    public ResponseEntity<byte[]> downloadMaterialTemplate(@RequestParam("materialType") String materialType,
                                                           @RequestParam("subjectIds") String subjectIds,
                                                           @RequestParam(value = "playType",required = false) String playType) throws Exception {
        return couMaterialBiz.downloadMaterialTemplate(materialType, subjectIds,playType);
    }

    /**
     * 执行导入素材文件
     * @param file 素材内容文件
     * @return Map<String, Object>
     */
    @RequestMapping(value = "/uploadMaterial", method = {RequestMethod.POST}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Map<String, Object> uploadMaterial(@RequestPart(value = "file") MultipartFile file,
                                              @RequestParam("userId") BigDecimal userId) {
        if (!validExtension(file.getOriginalFilename())) {
            return ResultUtil.ERROR("不支持的文件格式");
        }
        try (InputStream is = file.getInputStream()) {
            Workbook workbook = new XSSFWorkbook(is);
            Sheet sheet = workbook.getSheetAt(0);
            if (sheet.getLastRowNum() < 2) {
                return ResultUtil.ERROR("模版内没有素材内容");
            }
            if (sheet.getLastRowNum() > 2002) {
                return ResultUtil.ERROR("xls表中的数据不能多于2000行");
            }
            String value = getMaterialType(sheet);
            if(value.equals(MaterialType.TYPE_KEY_VIDEO.getValue())){
                return videoOrAudio(sheet, MaterialType.TYPE_KEY_VIDEO.getKey(),userId);
            }else if(value.equals(MaterialType.TYPE_KEY_LIVE.getValue())){
                return videoOrAudio(sheet, MaterialType.TYPE_KEY_LIVE.getKey(),userId);
            }else if(value.equals(MaterialType.TYPE_KEY_ARTICLE.getValue())){
                return videoOrAudio(sheet, MaterialType.TYPE_KEY_ARTICLE.getKey(),userId);
            }else{
                return ResultUtil.ERROR("不支持的素材类型");
            }
        } catch (Exception e) {
            logger.error("-------error",e);
            return ResultUtil.ERROR("上传失败");
        }
    }



    /**
     * 查询今日直播素材数据
     * @return PageInfo<CouMaterial>
     */
    @RequestMapping("/findBacklogLiveList")
    public  List<CouMaterial> findBacklogLiveList(@RequestParam("start") String start,@RequestParam("end") String end){

        return  couMaterialBiz.findBacklogLiveList(start,end);
    }

    /**
     * 查询素材
     * @param whereSql
     * @return
     */
    @RequestMapping("findCouMaterialBySql")
    public CouMaterial findCouMaterialBySql(@RequestParam("whereSql") String whereSql){
        CouMaterial couMaterial = couMaterialBiz.findOne(whereSql,null);
        return couMaterial;
    }



    /**
     * 判断文件格式
     * @param filename
     * @return
     */
    private boolean validExtension(String filename) {
        return filename.endsWith(".xlsx");
    }

    /**
     * 获取导入素材集合List
     * @param sheet 表的工作页
     * @param typeKey  素材类型，
     * @return
     */
    private Map<String,Object> videoOrAudio(Sheet sheet, String typeKey,BigDecimal userId) {
        List<CouMaterial> couMaterials = new LinkedList<>();
        String subjectIds = sheet.getRow(0).getCell(3).getStringCellValue().trim();
        String playKey = "";
        if(!MaterialType.TYPE_KEY_ARTICLE.getKey().equals(typeKey)){
            playKey = sheet.getRow(0).getCell(5).getStringCellValue().trim();
        }
        Row row;
        for (int i = 2; i <= sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            if (row != null) {
                CouMaterial couMaterial = new CouMaterial();
                Cell cell = row.getCell(0);
                couMaterial.setName(getCellValue(cell));//素材名称
                cell = row.getCell(1);
                couMaterial.setTeacherId(new BigDecimal(getCellValue(cell)));//讲师ID
                if(!MaterialType.TYPE_KEY_ARTICLE.getKey().equals(typeKey)){
                    cell = row.getCell(2);
                    couMaterial.setPlayUrl(getCellValue(cell).replace(" ",""));//视频码\音频码\直播间识别码
                    couMaterial.setPlayType(playKey);
                }
                if(MaterialType.TYPE_KEY_LIVE.getKey().equals(typeKey)) {
                    Optional<String> optional = setLiveData(row,couMaterial,playKey);
                    if(optional.isPresent()){
                        return ResultUtil.ERROR(optional.get());
                    }
                }
                if(MaterialType.TYPE_KEY_ARTICLE.getKey().equals(typeKey)) {
                    setArticleData(row,cell,couMaterial);
                }
                setSharedData(couMaterial,subjectIds, userId, typeKey);
                Optional<String> optional = checkMaterial(couMaterial);
                if (optional.isPresent()) {
                    return ResultUtil.ERROR(optional.get());
                }
                couMaterials.add(couMaterial);
            }
        }
        return couMaterialBiz.uploadMaterial(couMaterials);
    }

    /**
     * 设置直播特有字段数据
     * @param row 行
     * @param couMaterial 素材对象
     * @param playKey 云服务商
     * @return
     */
    private Optional<String> setLiveData(Row row,CouMaterial couMaterial,String playKey){
        Cell cell =null;
        if(playKey.equals("CC")){
            cell = row.getCell(3);
            couMaterial.setStudentCode(getCellValue(cell));//学员参加码
            try {
                cell = row.getCell(4);
                couMaterial.setLiveBeginTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(getCellValue(cell)));//直播开始时间
                cell = row.getCell(5);
                couMaterial.setLiveEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(getCellValue(cell)));//直播开始时间
            }catch (Exception e){
                return Optional.of("时间格式不正确");
            }
        }else{
            cell = row.getCell(3);
            couMaterial.setRoomId(getCellValue(cell));//直播间房间ID
            cell = row.getCell(4);
            couMaterial.setTeacherCode(getCellValue(cell));//讲师参加码
            cell = row.getCell(5);
            couMaterial.setStudentCode(getCellValue(cell));//学员参加码
            cell = row.getCell(6);
            couMaterial.setAdminCode(getCellValue(cell));//管理员参加码
            try {
                cell = row.getCell(7);
                couMaterial.setLiveBeginTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(getCellValue(cell)));//直播开始时间
                cell = row.getCell(8);
                couMaterial.setLiveEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(getCellValue(cell)));//直播开始时间
            }catch (Exception e){
                return Optional.of("时间格式不正确");
            }
        }
        return Optional.empty();
    }

    /**
     * 设置文章特有字段数据
     * @param row 行
     * @param cell 单元格
     * @param couMaterial 素材对象
     * @return
     */
    private void setArticleData(Row row,Cell cell,CouMaterial couMaterial){
        cell = row.getCell(2);
        couMaterial.setArticleSummary("<p>"+getCellValue(cell)+"</p>");//文章简介
        cell = row.getCell(3);
        couMaterial.setArticleContext("<p>"+getCellValue(cell)+"</p>");//文章内容
    }

    /**
     * 素材共享字段
     * @param couMaterial 素材对象
     * @param subjectIds 专业/科目ID串
     * @param userId 创建用户ID
     * @param typeKey 直播LIVE/视频VIDEO/音频AUDIO/文章ARTICLE
     */
    private void setSharedData(CouMaterial couMaterial, String subjectIds, BigDecimal userId, String typeKey){
        couMaterial.setMouldId(new BigDecimal(0));//隶属模块ID
        couMaterial.setSubjectIds(subjectIds);//专业/科目ID串
        couMaterial.setCreateUserId(userId);//创建用户ID
        couMaterial.setTypeKey(typeKey);//素材类型 直播LIVE/视频VIDEO/音频AUDIO/文章ARTICLE
        couMaterial.setPlayStatus(1);//播放状态 正常/冻结
        couMaterial.setStatus(2);//素材状态 已发布/未发布/删除
    }

    /**
     * 获取素材类型
     * @param sheet
     * @return
     */
    private String getMaterialType(Sheet sheet) {
        Row row = sheet.getRow(0);
        if (row != null) {
            Cell cell = row.getCell(1);
            if (cell != null) {
                return cell.getStringCellValue().trim();
            }
        }
        return "";
    }

    /**
     * 获取表格单元格的值
     * @param cell
     * @return
     */
    private String getCellValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cell.getCellTypeEnum()) {
            case NUMERIC:
                return "" + cell.getNumericCellValue();
            case STRING:
                return cell.getStringCellValue();
            case BOOLEAN:
                return "" + cell.getBooleanCellValue();
            default:
                return "";
        }
    }

    /**
     * 校验素材数据
     *
     * @param material 素材
     */
    private Optional<String> checkMaterial(CouMaterial material) {
        if (StringUtils.isEmpty(material.getName())) {
            return Optional.of("请填写素材标题");
        }
        if (material.getName().length() > 30) {
            return Optional.of("素材名称不能超过30个字符");
        }
        if (StringUtils.isEmpty(material.getTypeKey())) {
            return Optional.of("请选择创建的素材类型");
        }
        if (StringUtils.isEmpty(material.getSubjectIds())) {
            return Optional.of("请填写素材的专业和科目id");
        } else if (material.getSubjectIds().split(",").length < 2) {
            return Optional.of("请同时指定素材的专业和科目id");
        }
        if (ObjectUtils.isEmpty(material.getTeacherId())) {
            return Optional.of("请选择一位素材讲师");
        }
        // 删除html标签。summernote默认 <p><br></p>
        if (MaterialType.TYPE_KEY_ARTICLE.getKey().equalsIgnoreCase(material.getTypeKey())) {
            if (StringUtils.isEmpty(StringUtils.deleteHtml(material.getArticleSummary()))) {
                return Optional.of("请填写文章类型素材的简介");
            } else if (StringUtils.isEmpty(StringUtils.deleteHtml(material.getArticleContext()))) {
                return Optional.of("请填写文章类型素材的内容");
            }
        }
        if (MaterialType.TYPE_KEY_LIVE.getKey().equalsIgnoreCase(material.getTypeKey())) {
            if (StringUtils.isEmpty(material.getPlayType())) {
                return Optional.of("请选择直播素材的视频服务商");
            } else if (StringUtils.isEmpty(material.getPlayUrl().trim())) {
                return Optional.of("请填写直播素材的视频码");
            } else if (material.getLiveBeginTime() == null) {
                return Optional.of("请指定直播开始时间");
            } else if (material.getLiveEndTime() == null) {
                return Optional.of("请指定直播结束时间");
            } else if (!material.getLiveBeginTime().before(material.getLiveEndTime())) {
                return Optional.of("直播开始时间应在结束时间之前");
            } else if (StringUtils.isEmpty(material.getStudentCode())) {
                return Optional.of("请添加直播间学生参加码");
            } else if (StringUtils.isEmpty(material.getTeacherCode()) && material.getPlayType().equals("96K")) {
                return Optional.of("请添加直播间讲师参加码");
            } else if (StringUtils.isEmpty(material.getAdminCode()) && material.getPlayType().equals("96K")) {
                return Optional.of("请添加直播间助教参加码");
            } else if (StringUtils.isEmpty(material.getRoomId()) && material.getPlayType().equals("96K")) {
                return Optional.of("请添加直播间ID");
            }
        }
        if (MaterialType.TYPE_KEY_AUDIO.getKey().equalsIgnoreCase(material.getTypeKey())) {
            if (StringUtils.isEmpty(material.getPlayType())) {
                return Optional.of("请选择音频素材的音频服务商");
            } else if (StringUtils.isEmpty(material.getPlayUrl().trim())) {
                return Optional.of("请填写音频素材的音频码");
            }
        }
        if (MaterialType.TYPE_KEY_VIDEO.getKey().equalsIgnoreCase(material.getTypeKey())) {
            if (StringUtils.isEmpty(material.getPlayType())) {
                return Optional.of("请选择视频素材的视频服务商");
            } else if (StringUtils.isEmpty(material.getPlayUrl().trim())) {
                return Optional.of("请填写视频素材的视频码");
            }
        }
        return Optional.empty();
    }
}
